package cn.sc.summer.mybatis.enums;

import cn.hutool.core.text.CharSequenceUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * The table structure is automatically updated with type, and the PO class in the code is used as the benchmark for database table comparison
 *
 * @author a-xin
 * @date 2024/8/26 14:01
 */
@Getter
@AllArgsConstructor
public enum TableUpdateStage {

    /**
     * Only database tables are added
     */
    ADD("ADD", "只做数据库表新增"),

    /**
     * Add and update database fields, create a new table if it does not exist, and not delete the table if the table structure does not exist in the database
     */
    UPDATE("UPDATE", "进行数据库字段添加更新，若不存在的表则新建，若数据库不存在表结构，不会删除"),

    /**
     * If the UPDATE function is included, you can delete tables that do not exist in the database
     */
    DEL("DEL", "包含UPDATE功能的同时，删除数据库中不存在的表"),
    ;

    /**
     * 数据源表结构更新策略标识
     */
    private final String type;

    /**
     * 更新策略描述
     */
    private final String desc;

    /**
     * 根据更新策略标识获取更新策略信息
     *
     * @param type 更新策略标识
     * @return 更新策略信息
     */
    public static TableUpdateStage getByType(String type) {
        if (CharSequenceUtil.isBlank(type)) {
            return null;
        }
        for (TableUpdateStage tableUpdateStage : TableUpdateStage.values()) {
            if (tableUpdateStage.type.equalsIgnoreCase(type)) {
                return tableUpdateStage;
            }
        }
        return null;
    }

}
